package com.bort.system.config;

import com.bort.system.BortApplication;
import com.bort.system.common.DataContainer;
import javafx.application.Platform;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.URL;

@Configuration
public class SystemConfig {

    @Autowired
    private ResourceLoader resourceLoader;

    @Bean
    void common(){
        try {
            Stage stage = BortApplication.getStage();
            if(SystemTray.isSupported()){
                Platform.setImplicitExit(false);
                SystemTray tray = SystemTray.getSystemTray();
                Resource resource = resourceLoader.getResource("classpath:" + "icon/logo.png");
                URL url = resource.getURL();
                Image image = Toolkit.getDefaultToolkit().getImage(url);
                image.getScaledInstance(64,64,Image.SCALE_SMOOTH);
                String title = "台球厅管理系统";

                PopupMenu menus = new PopupMenu();
                MenuItem open = new MenuItem("显示主界面");
                MenuItem exit = new MenuItem("退出程序");
                menus.add(open);
                menus.add(exit);


                TrayIcon trayIcon = new TrayIcon(image,title,menus);
                trayIcon.setImageAutoSize(true);

                open.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                stage.show();
                                stage.setIconified(false);
                            }
                        });
                    }
                });

                exit.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        Platform.setImplicitExit(true);
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                stage.close();
                            }
                        });
                        tray.remove(trayIcon);
                    }
                });

                trayIcon.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                stage.show();
                                stage.setIconified(false);
                            }
                        });
                    }
                });
                tray.add(trayIcon);
                DataContainer.trayIcon = trayIcon;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (AWTException e){
            e.printStackTrace();
        }
    }
}
